/**
 * @ author: 汪孟苹
 */
define(["jquery", "bbcare"], function ($, $bb) {
    var attention = {};
    
    /**
     * 校验规则
     */
    var noNull = function(params){              // 非空
        attentionNoNull(params.obj);
    }
    var attentionNoNull = function(obj){
        var $label = obj.parents(".qaCard").find("label.control-label");
        if($label.find("i.not-null").length == 0){
            $label.prepend('<i class="not-null">*</i>');
        }
    }

    var num = function(params){              // 整数+小数
        var _default = {
            "ruleVal":true,
        };
        params = $.extend(_default,params);
        var reg = /^$|^-?\d+(\.\d+)?$/;
        var success = $.parseJSON(params.ruleVal) ? reg.test(params.value) : true;
        attentionTip(success,params.obj);
    }
    var moreThanNum = function(params){     // 大于一个数   
        var _default = {
            "ruleVal":0,
        };
        params = $.extend(_default,params);          
        if( params.value == "" ) return true;
        var success = Number(params.value) > Number(params.ruleVal) ? true : false;
        attentionTip(success,params.obj);
    }
    var lessThanNum = function(params){     // 小于一个数   
        var _default = {
            "ruleVal":0,
        };
        params = $.extend(_default,params);          
        if( params.value == "" ) return true;
        var success = Number(params.value) < Number(params.ruleVal) ? true : false;
        attentionTip(success,params.obj);
    }
    /**
     * 没通过校验的提示信息
     */
    var attentionTip = function(success,obj){
        if(!success){
            obj.parents(".qaCard").addClass("has-warning");
        }else{
            obj.parents(".qaCard").removeClass("has-warning");
        }
    }
    
    /**
     * 校验处理
     */
    var attentionHandle = function(obj){
        var attentionJson = obj.attr("data-attention");
        if(!attentionJson || attentionJson.trim().length == 0) return true;
    	var options = JSON.parse( attentionJson );
		var value = obj.val(); // 获取输入的值
        if(value) value = value.trim();
        if( typeof(obj.attr("type")) != "undefined" ){
            var type = obj.attr("type");
            if( type == "radio" ){
                value = obj.parents(".radio").find("input[type=radio]:checked").length;
            }else if( type == "checkbox" ){
                value = obj.parents(".checkbox").find("input[type=checkbox]:checked").length;
            }
        }
		if(!options || options.length == 0) return true;
        for (var i = 0; i < options.length; i++) {
            var params = {
                "value":value,
                "ruleVal":options[i].val,
                "obj":obj,
            }
            switch(options[i].rule) {
                case "noNull":
                    noNull(params);
                    break;
                case "num":
                    num(params);
                    break;
                case "moreThanNum":
                    moreThanNum(params);
                    break;
                case "lessThanNum":
                    lessThanNum(params);
                    break;
            }
        }
    }
    
    /**
     * 失去焦点时触发
     */
    attention.blurInit = function(obj){
        attentionHandle(obj);
    }

    /**
     * 页面初始化时触发
     */
    attention.init = function(){
        $('.validatebox').each(function(i){
            var obj = $('.validatebox').eq(i);
            attentionHandle(obj);
        });
    }

    return attention;
});